﻿using Model.CommonEntities;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Common.DrawPicture
{
    public static class ChartFunction
    {
        /// <summary>
        /// 针对特定控件生成Chart
        /// </summary>
        /// <param name="chart1"></param>
        /// <param name="DrawData"></param>
        public static void DrawChart(Chart chart1,TotalResult DrawData = null)
        {
            chart1.Series.Clear();


            #region 表格ChartAreas区域样式
            var chart = chart1.ChartAreas[0];
            chart.AxisX.IntervalType = DateTimeIntervalType.Number;

            chart.AxisX.LabelStyle.Format = "";
            chart.AxisY.LabelStyle.Format = "";
            chart.AxisY.LabelStyle.IsEndLabelVisible = true;
            
            chart.AxisX.Minimum = 0;
            //chart.AxisX.Maximum = 100;
            chart.AxisX.Interval = 15;
            chart.AxisY.Interval = 2;
            chart.AxisY.Maximum = (double)(DrawData.StockPriseList.Max() > DrawData.StockReturnList.Max() == true ? DrawData.StockPriseList.Max() : DrawData.StockReturnList.Max());
            chart.AxisY.Minimum = (double)(DrawData.StockPriseList.Min() < DrawData.StockReturnList.Min() == true ? DrawData.StockPriseList.Min() : DrawData.StockReturnList.Min());
            #endregion


            //chart1.Series.Add("占位符");
            //不显示第i条序列线
            //chart1.Series[0].IsVisibleInLegend = true;

            #region 折线图样式
            chart1.Series.Add("收益");
            chart1.Series.Add("股价");
            //绘制曲线图
            //chart1.Series["收益"].ChartType = SeriesChartType.Spline;
            //绘制直线图
            chart1.Series["收益"].ChartType = SeriesChartType.Line;
            chart1.Series["股价"].ChartType = SeriesChartType.Line;
            chart1.Series["收益"].Color = Color.Red;
            chart1.Series["股价"].Color = Color.Blue;
            #endregion

            #region 结果摘要信息
            chart1.Series.Add("回报:" + DrawData.MyTotalReturn);
            chart1.Series.Add("自然:" + DrawData.ObjectiveTotalReturn);
            chart1.Series["回报:" + DrawData.MyTotalReturn].ChartType = SeriesChartType.Point;
            chart1.Series["自然:" + DrawData.ObjectiveTotalReturn].ChartType = SeriesChartType.Point;
            chart1.Series["回报:" + DrawData.MyTotalReturn].Color = Color.Red;
            chart1.Series["自然:" + DrawData.ObjectiveTotalReturn].Color = Color.Blue;
            #endregion


            //序列数据绑定到对应Series上
            for (int i = 0; i < DrawData.TList.Count; i++)
            {
                chart1.Series["收益"].Points.AddXY(i, DrawData.StockReturnList[i]);
                chart1.Series["股价"].Points.AddXY(i, DrawData.StockPriseList[i]);
            }

            #region 其他样式
            //坐标轴说明
            //chart.AxisY.Title = "prise";
            //chart.AxisX.Title = "time";
            //chart1.ChartAreas[0].AxisX.Title = "hh";
            //chart1.Series[0].LegendText = "223";
            //标题
            //chart1.Titles.Add("1122");
            //标签悬浮提示
            //chart1.Series[0].LegendToolTip = DrawData.MyTotalReturn;
            #endregion
        }

        /// <summary>
        /// 动态生成一个Chart,可填充到其他插件中
        /// </summary>
        /// <param name="strategy">策略名称</param>
        /// <param name="code">代码</param>
        /// <param name="x">当前图片的x坐标</param>
        /// <param name="y">当前图片的x坐标</param>
        /// <param name="DrawData">策略结果数据</param>
        /// <returns></returns>
        public static Chart DynamicChart(string strategy, string code, int x, int y, TotalResult DrawData = null)
        {
            Chart newchart = new Chart();

            

            newchart.ChartAreas.Add("chartarea2");
            newchart.ChartAreas[0].Position.Auto = true;

            #region 表格ChartAreas区域样式
            var chart = newchart.ChartAreas[0];
            chart.AxisX.IntervalType = DateTimeIntervalType.Number;

            chart.AxisX.LabelStyle.Format = "";
            chart.AxisY.LabelStyle.Format = "";
            chart.AxisY.LabelStyle.IsEndLabelVisible = true;

            chart.AxisX.Minimum = 0;
            //chart.AxisX.Maximum = 100;
            chart.AxisX.Interval = 15;
            chart.AxisY.Interval = 2;
            chart.AxisY.Maximum = (double)(DrawData.StockPriseList.Max() > DrawData.StockReturnList.Max() == true ? DrawData.StockPriseList.Max() : DrawData.StockReturnList.Max());
            chart.AxisY.Minimum = (double)(DrawData.StockPriseList.Min() < DrawData.StockReturnList.Min() == true ? DrawData.StockPriseList.Min() : DrawData.StockReturnList.Min());
            #endregion


            //chart1.Series.Add("占位符");
            //不显示第i条序列线
            //chart1.Series[0].IsVisibleInLegend = true;

            #region 折线图样式
            newchart.Series.Add("收益");
            newchart.Series.Add("股价");
            

            //绘制曲线图
            //chart1.Series["收益"].ChartType = SeriesChartType.Spline;
            //绘制直线图
            newchart.Series["收益"].ChartType = SeriesChartType.Line;
            newchart.Series["股价"].ChartType = SeriesChartType.Line;
            newchart.Series["收益"].Color = Color.Red;
            newchart.Series["股价"].Color = Color.Blue;
            #endregion

            #region 结果摘要信息
            newchart.Series.Add("回报:" + DrawData.MyTotalReturn);
            newchart.Series.Add("自然:" + DrawData.ObjectiveTotalReturn);
            newchart.Series["回报:" + DrawData.MyTotalReturn].ChartType = SeriesChartType.Point;
            newchart.Series["自然:" + DrawData.ObjectiveTotalReturn].ChartType = SeriesChartType.Point;
            newchart.Series["回报:" + DrawData.MyTotalReturn].Color = Color.Red;
            newchart.Series["自然:" + DrawData.ObjectiveTotalReturn].Color = Color.Blue;
            #endregion


            //序列数据绑定到对应Series上
            for (int i = 0; i < DrawData.TList.Count; i++)
            {
                newchart.Series["收益"].Points.AddXY(i, DrawData.StockReturnList[i]);
                newchart.Series["股价"].Points.AddXY(i, DrawData.StockPriseList[i]);
            }

            #region 其他样式
            newchart.Width = 537;
            newchart.Height = 200;
            newchart.Location = new Point(x, y);
            
            newchart.Legends.Add("legend2");

            //坐标轴说明
            //chart.AxisY.Title = "prise";
            //chart.AxisX.Title = "time";
            //chart1.ChartAreas[0].AxisX.Title = "hh";
            //newchart.Series[0].LegendText = strategy + DrawData.StockCode;
            //标题
            newchart.Titles.Add(code + strategy);
            //标签悬浮提示
            //chart1.Series[0].LegendToolTip = DrawData.MyTotalReturn;
            #endregion
            return newchart;
        }
    }
}
